﻿(function (ko, datacontext) {
    datacontext.book = book;

    function book(data) {
        var self = this;
        data = data || {};

        // Persisted properties
        self.bookId = data.bookId;
        self.userId = data.userId || "to be replaced";

        self.title = ko.observable(data.title || "book name");
        self.author = ko.observable(data.author || "book author");
        self.description = ko.observable(data.description || "boodsd lkfajslkba;n alksdj v;lakjb;la n;aoldkj b;lakdjfb; lakdfjhb ;alkdfhjb ;aldkfnb;aoinbro;ae ihrb");
        self.genre = ko.observable(data.genre || "sci-fi, fantasy");
        self.rating = ko.observable(data.rating || 0);
        
        self.canEdit = ko.observable(false);
        self.isEditing = ko.observable(false);

        self.newbookTitle = ko.observable();
        self.errorMessage = ko.observable();

        // Auto-save when these properties change
        self.title.subscribe(function () {
            return datacontext.saveChangedBook(self);
        });
        self.author.subscribe(function () {
            return datacontext.saveChangedBook(self);
        });
        self.description.subscribe(function () {
            return datacontext.saveChangedBook(self);
        });
        self.genre.subscribe(function () {
            return datacontext.saveChangedBook(self);
        });
        self.rating.subscribe(function () {
            return datacontext.saveChangedBook(self);
        });
        

        self.toJson = function () { return ko.toJSON(self); };
    };
    
})(ko, bookApp.datacontext);